<template>
    <div>
        <input type="text" v-model="title">
        <textarea cols="30" rows="10" v-model="con"></textarea>
        <button @click="cur">确定</button>
    </div>
</template>

<script>
export default {
    created(){
        let blogCon = localStorage.getItem('blogCon') && JSON.parse(localStorage.getItem('blogCon'));
        this.title = blogCon.title;
        this.con = blogCon.con;
        this.id = blogCon.id;
    },
    data(){
        return {
            title:'',
            con:'',
            id:''
        }
    },
    methods:{
        //确定按钮
        cur(){
            let url = '';
            let type = '';
            if(this.id){
                //编辑
                url = '/api/blog/'+this.id;
                type = 'put';
            }else{
                //添加
                url = '/api/blog';
                type = 'post';
            }
            //容错
            if(this.title && this.con){
                let username = localStorage.getItem('username');
                this.$http[type](url,{title:this.title,con:this.con,username,id:this.id}).then(res => {
                    if(res.data.code === 1){
                        localStorage.removeItem('blogCon');
                        this.$router.push('/home');
                    }
                })
            }else{
                alert('参数有误')
            }
        }
    }
}
</script>